package com.xml.dom.write;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVReader {

	/**
	 * getting Data Map List from a CSV file
	 * 
	 * @param path
	 *            String
	 * @return List
	 * @throws IOException
	 */
	public List<Map<String, String>> read(String path) throws IOException {

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String line = null;

		while ((line = reader.readLine()) != null) {
			list.add(getDataMap(line));
		}

		return list;
	}

	/**
	 * getting Data Map from csv String
	 * 
	 * @param line
	 *            String
	 * @return Map
	 */
	private Map<String, String> getDataMap(String line) {
		Map<String, String> map = new HashMap<String, String>();
		String[] strs = line.split(",");
		for (int i = 0; i < strs.length; i++) {
			switch (i) {
			case 0:
				map.put("name", strs[i]);
				break;
			case 1:
				map.put("price", strs[i]);
				break;
			case 2:
				map.put("author", strs[i]);
				break;
			default:
				break;
			}
		}
		return map;
	}

}
